﻿using Cinch;
using MEFedMVVM.ViewModelLocator;
using System;
using System.ComponentModel.Composition;
using FR.SAFTAnalyserLite.Services;
using FR.SAFTAnalyserLite.ViewModels.Pane;

namespace FR.SAFTAnalyserLite.ViewModels
{
    [ExportViewModel("SettingsViewModel")]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class SettingsViewModel : DocumentPartViewModel
    {
        IOpenFileService openFileService;

        public SettingsViewModel()
            : base("Opções")
        {
            this.openFileService = ServiceResolver.Instance.OpenFileService;

            //Commands
            DoOpenSaftFileCommand = new SimpleCommand<Object, Object>(CanOpenSaftFile, OpenSaftFile);
            DoOpenPublicKeyFileCommand = new SimpleCommand<Object, Object>(CanOpenPublicKeyFile, OpenPublicKeyFile);
        }

        public string SAFTFileName
        {
            get { return Workspace.Workspace.Instance.Config.SAFTFileName; }
            set
            {
                Workspace.Workspace.Instance.Config.SAFTFileName = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<SettingsViewModel>(x => x.SAFTFileName));
            }
        }

        public string PublicKeyFileName
        {
            get { return Workspace.Workspace.Instance.Config.PublicKeyFileName; }
            set
            {
                Workspace.Workspace.Instance.Config.PublicKeyFileName = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<SettingsViewModel>(x => x.PublicKeyFileName));
            }
        }

        public string PrivateKeyFileName
        {
            get { return Workspace.Workspace.Instance.Config.PrivateKeyFileName; }
            set
            {
                Workspace.Workspace.Instance.Config.PrivateKeyFileName = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<SettingsViewModel>(x => x.PrivateKeyFileName));
            }
        }

        public bool RememberLastOpenFiles
        {
            get { return Workspace.Workspace.Instance.Config.RememberLastOpenFiles; }
            set
            {
                Workspace.Workspace.Instance.Config.RememberLastOpenFiles = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<SettingsViewModel>(x => x.RememberLastOpenFiles));
            }
        }

        public DateTime DataMinima
        {
            get { return Workspace.Workspace.Instance.Config.MinDate; }
            set
            {
                Workspace.Workspace.Instance.Config.MinDate = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<SettingsViewModel>(x => x.DataMinima));
            }
        }

        public int FontSize
        {
            get { return Workspace.Workspace.Instance.Config.FontSize; }
            set
            {
                Workspace.Workspace.Instance.Config.FontSize = value;
                App.Current.MainWindow.FontSize = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<SettingsViewModel>(x => x.FontSize));
            }
        }

        public SaftTablesDescription[] AllSaftTables
        {
            get { return Workspace.Workspace.Instance.Config.OpenedTables; }
            set
            {
                Workspace.Workspace.Instance.Config.OpenedTables = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<SettingsViewModel>(x => x.AllSaftTables));
            }
        }

        #region Commands
        public SimpleCommand<object, object> DoOpenSaftFileCommand { get; private set; }
        public void OpenSaftFile(Object args)
        {
            if (openFileService != null)
                this.SAFTFileName = openFileService.OpenFile("SAFT Files (*.xml)|*.xml");
        }
        public bool CanOpenSaftFile(Object args)
        {
            return RememberLastOpenFiles;
        }

        public SimpleCommand<object, object> DoOpenPublicKeyFileCommand { get; private set; }
        public void OpenPublicKeyFile(Object args)
        {
            if (openFileService != null)
                this.PublicKeyFileName = openFileService.OpenFile("Public Key files (.txt) | *.txt");
        }
        public bool CanOpenPublicKeyFile(Object args)
        {
            return RememberLastOpenFiles;
        }
        #endregion Commands
    }
}
